package com.zr.stock.stockIdentification.controller;

import com.zr.stock.model.Stockidentification;
import com.zr.stock.stockIdentification.modelVo.IdentificationAddVo;
import com.zr.stock.stockIdentification.modelVo.IdentificationQueryVo;
import com.zr.stock.stockIdentification.modelVo.IdentificationUpdateVo;
import com.zr.stock.stockIdentification.modelVo.UpdateStatusVO;
import com.zr.stock.stockIdentification.service.IdentificationService;
import com.zr.stock.utils.AllRecords;
import com.zr.stock.utils.ResultVO;
import com.zr.stock.utils.ResultVOBuilder;
import com.zr.stock.utils.Status;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.security.PolicySpi;
import java.util.ArrayList;
import java.util.List;

@RestController
public class IdentificationController {
    @Autowired
    private IdentificationService identificationService;
    //查询库房标识
    @PostMapping("warehouseTag/queryPage")
    @CrossOrigin
    public ResultVO<AllRecords<Stockidentification>> warehouseTagQueryPage(@RequestBody IdentificationQueryVo identificationQueryVo){
        return identificationService.queryPage(identificationQueryVo);
    }
    //状态下拉列表
    @PostMapping("iwm/getPullDownList")
    @CrossOrigin
    public ResultVO<List<Status>> getPullDownList(){
        List<Status> statuses = new ArrayList<>();
        Status status1=new Status("启用",true);
        Status status2=new Status("禁用",false);
        statuses.add(status1);
        statuses.add(status2);
        return ResultVOBuilder.success(statuses);
    }
    //新增
    @PostMapping("warehouseTag/add")
    @CrossOrigin
    public ResultVO<IdentificationAddVo> warehouseTagAdd(@RequestBody @Valid IdentificationAddVo identificationAddVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return identificationService.warehouseTagAdd(identificationAddVo);
    }

    @PostMapping("warehouseTag/updateStatus")
    @CrossOrigin
    public ResultVO<UpdateStatusVO> updateStatus(@RequestBody UpdateStatusVO updateStatusVO){
        return identificationService.updateStatus(updateStatusVO);
    }
    @PostMapping("warehouseTag/update")
    @CrossOrigin
    public ResultVO<IdentificationUpdateVo> warehouseTagUpdate(@RequestBody @Valid  IdentificationUpdateVo identificationUpdateVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return identificationService.warehouseTagUpdate(identificationUpdateVo);
    }
}
